from flask import Flask,request
import json
import re
import pymysql

app = Flask(__name__)


@app.route("/jd",methods=["POST"])
def login():
    return_request = {'code':'200','return_msg':'处理成功','result':False}
    if request.get_data() is None:
        return_request['code'] = '10001'
        return_request['return_msg'] = '请求参数为空'
        return json.dumps(return_request,ensure_ascii=False)
    get_Data = request.get_data()
    print(get_Data)
    data = get_Data.decode("utf-8")
    print(data)
    if '【' not in data and '】' not in data and '码' not in data:
        return_request['code'] = '10001'
        return_request['return_msg'] = '请求参数为空'
        return json.dumps(return_request,ensure_ascii=False)
    kd = re.findall(r'【(.*?)】',data,re.M)[0]
    code = re.findall(r'(\d{6})',data,re.M)[0]
    t = re.search('(\d{4}-\d{1,}-\d{1,}.\d{1,}.\d{1,})',data).group(1)
    print(kd, code, t)
    sql = 'insert into verification_code(id,courier,code,datatime,content) values(null,%s,%s,%s,%s)'
    values = (kd, code, t, data)
    cur.execute(sql,values)
    connet.commit()
    return json.dumps(return_request,ensure_ascii=False)


@app.route("/test",methods=["get"])
def lo():
    return_request = {'code':'200','return_msg':'处理成功','result':False}
    return json.dumps(return_request,ensure_ascii=False)


if __name__ == '__main__':
    connet = pymysql.connect(host='192.168.0.14',port=3306,user='junfeng',passwd='Gzjunfeng888!',db='jf_python_db')
    cur = connet.cursor()
    app.run(host="0.0.0.0",port='5007')